package com.loopy.servlet.request;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "HttpServletRequestMethods", value = "/requestMethods")
public class HttpServletRequestMethods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("HttpServletRequestMethods 的doPost()方法被调用");
        System.out.println("请求的资源路径URI="+request.getRequestURI());
        System.out.println("请求的同统一资源定位符URL="+request.getRequestURL());
        System.out.println("请求的客户端IP地址="+request.getRemoteAddr());
        System.out.println("请求头host="+request.getHeader("Host"));
//        System.out.println("请求访问的浏览器是="+request.getHeader("User-agent"));
        String userAgent = request.getHeader("User-agent");
        String[] s = userAgent.split(" ");
        System.out.println("请求访问的浏览器是="+s[s.length-1].split("\\/")[0]);
        System.out.println("请求方式="+request.getMethod());
        //获取表单数据,解决接收参数的中文乱码问题
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println("hobby="+hobby);
        }
        System.out.println("username="+username);
        System.out.println("password="+password);
//        String cookie = request.getHeader("Cookie");
        System.out.println("cookie="+request.getSession().getId());

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>用户名"+username+"</h1>");
        writer.flush();
        writer.close();

    }
}
